package com.hqx.gulimall.product.service.impl;

import com.hqx.gulimall.product.dao.BrandDao;
import com.hqx.gulimall.product.dao.CategoryDao;
import com.hqx.gulimall.product.entity.BrandEntity;
import com.hqx.gulimall.product.entity.CategoryEntity;
import com.hqx.gulimall.product.vo.BrandRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqx.common.utils.PageUtils;
import com.hqx.common.utils.Query;

import com.hqx.gulimall.product.dao.CategoryBrandRelationDao;
import com.hqx.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hqx.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryBrandRelationList(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        return this.list(queryWrapper);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandRespVo> getBrandListByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        return categoryBrandRelationEntityList.stream().map(categoryBrandRelationEntity -> {
            BrandRespVo brandRespVo = new BrandRespVo();
            BeanUtils.copyProperties(categoryBrandRelationEntity,brandRespVo);
            return brandRespVo;
        }).collect(Collectors.toList());
    }

}